﻿#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<iostream>
using namespace std;

enum Color
{
	RED,
	BLACK
};

//Key-Value 结构    RBTreeNode 节点结构
template<class K,class V>
struct RBTreeNode
{
	pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Color _col;

	//参数传的是const pair<K,V>& kv
	RBTreeNode(const pair<K, V>& kv)
		:_kv(kv),
		_left(nullptr),
		_right(nullptr),
		_parent(nullptr)
	{}
};


template<class K,class V>
struct RBTree
{
	typedef RBTreeNode<K, V> Node;
public:
	bool Insert(const pair<K, V>& kv)
	{
	//如果是空树就直接赋值节点   并把根节点的颜色置为黑
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}

			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}

			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		//新增节点   颜色给红色
		cur->_col = RED;
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
	
		//对父亲进行链接
		cur->_parent = parent;


		//只有当父亲节点parent不为空   并且parent的颜色为红才进循环进行处理
		while (parent && parent->_col == RED) 
		{
			//对于这个循环   如果父亲是黑直接返回    只有当父亲是红才进行循环(出现了连续的红色节点)
			Node* grandfather = parent->_parent;
			//确定叔叔
		//情况一   
			if (parent == grandfather ->_left)
			{
				Node* uncle = grandfather->_right;
				//条件   这是小情况1  叔叔存在并且叔叔的颜色是红色
				if (uncle && uncle->_col == RED)
				{
				 // 只变色
				 //      g
				 //  p       u
					//变色步骤 
					//只需要变色然后往上面处理
					//把父亲和叔叔的颜色转黑
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					//然后继续往上处理
					cur = grandfather;
					parent = cur->_parent;
				}

			   //条件     这是小情况2   叔叔存在但是叔叔
				else
				{
				
					if (cur == parent->_left)
					{
					 // 单旋+变色
					 //       g
					 //   p       u
				     //c
						//进行对爷爷的位置  右单旋
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
					// 双旋+变色
					//       g
					//   p       u
					//		c
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;   //cur变为了根
						grandfather->_col = RED;
					}
					break;
				}
			}

		//情况二
			//     g
			// u       p
			else
			{
				Node* uncle = grandfather->_left;
				//叔叔存在且为红    进行变色
				if (uncle && uncle->_col == RED)
				{
					//变色
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
				
					//继续往上处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else  
				{
					//叔叔不存在   或者存在且为黑
					// 单旋+变色
					//        g
					//     u     p
					//				c
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						//      g
						//  u       p
						//       c  
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}
		//防止根节点不是黑色   在这进行额外处理一下  确保结束一定是黑为根
		_root->_col = BLACK;
		return true;
	}



	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		// 需要注意除了要修改孩子指针指向，还要修改父亲 
		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;
		Node* parentParent = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;

		// parent有可能是整棵树的根，也可能是局部的子树 
		// 如果是整棵树的根，要修改_root 
		// 如果是局部的指针要跟上一层链接 
		if (parentParent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subL;
			}
			else
			{
				parentParent->_right = subL;
			}
			subL->_parent = parentParent;
		}
	}


	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;
		Node* parentParent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;

		if (parentParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}
	}


	//检查树节点是否是正确的格式
	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			//前序遍历走到空时   意味着一条路径走完
			//cout<<blackNum<<endl;
			if (refNum != blackNum)
			{
				cout << "存在黑色节点的数量不等的路径" << endl;
				return false;
			}
			return true;
		}

		//检查孩子不方便  因为孩子有两个且不一定存在  所以反过来检查父亲
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红色节点" << endl;
			return false;
		}
		if (root->_col == BLACK)
		{
			++blackNum;
		}
		return Check(root->_left, blackNum, refNum) && Check(root->_right, blackNum, refNum);
	}


	//检查是否是平衡树
	bool IsBalanceTree()
	{
		if (_root == nullptr)
		{
			return true;
		}

		if (_root->_col == RED)
		{
			return false;
		}

		//參考值
		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, refNum);
	}


	//红黑树的搜素
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}


	//中序遍历
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

private:
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_kv.first << " : " << root->_kv.second << endl;
		_InOrder(root->_right);
	}

private:
	Node* _root = nullptr;
};